package com.cdn.allmobilestore.object;

import java.util.ArrayList;
import java.util.Calendar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.cdn.allmobilestore.ulti.Constant;

public class App {

	private String id;
	private String name;
	private String iconLink;
	private int rateNum;
	private int price;
	private String companyName;
	private String description;
	private String detail;
	private String linkApk;
	private int downloadNum;
	private float size;
	private Calendar uploadDate;
	private ArrayList<App> relativeApps;
	private ArrayList<String> images;
	private Boolean isPay;
	private Boolean isOnline;
	private Boolean isHot;
	private String packageName;
	private String version;

	public App() {
		// TODO Auto-generated constructor stub
	}

	public App(String id, String name, String iconLink, int rateNum, int price,
			String companyName, String description, String linkApk,
			int downloadNum, float size, Calendar uploadDate,
			ArrayList<App> relativeApps, ArrayList<String> images) {
		super();
		this.id = id;
		this.name = name;
		this.iconLink = iconLink;
		this.rateNum = rateNum;
		this.price = price;
		this.companyName = companyName;
		this.description = description;
		this.linkApk = linkApk;
		this.downloadNum = downloadNum;
		this.size = size;
		this.uploadDate = uploadDate;
		this.relativeApps = relativeApps;
		this.images = images;
	}

	public App(JSONObject json) {
		super();
		initFromJson(json);
	}

	public void initFromJson(JSONObject json) {
		try {
			this.id = json.getString(Constant.id);
			this.name = json.getString(Constant.app_name);
			this.iconLink = json.getString(Constant.iconLink);
			this.rateNum = json.getInt(Constant.rateNum);
			this.price = json.getInt(Constant.is_pay);
			this.companyName = json.getString(Constant.company);
			this.linkApk = json.getString(Constant.linkApk);
			this.downloadNum = json.getInt(Constant.downloaded);
			this.size = Float.parseFloat(json.getString(Constant.file_size));
			this.isHot = json.getInt(Constant.is_hot) == 1 ? true : false;
			this.isPay = json.getInt(Constant.is_pay) == 1 ? true : false;
			this.isOnline = json.getInt(Constant.is_online) == 1 ? true : false;

			if (json.has(Constant.package_name)) {
				this.packageName = json.getString(Constant.package_name);
			}
			if (json.has(Constant.version)) {
				this.version = json.getString(Constant.version);
			}
			if (json.has(Constant.description)) {
				this.description = json.getString(Constant.description);
			}
			if (json.has(Constant.detail)) {
				this.detail = json.getString(Constant.detail);
			}
			if (json.has(Constant.listOfImage)) {
				JSONArray arrayImages = json.getJSONArray(Constant.listOfImage);
				ArrayList<String> images = new ArrayList<String>();
				for (int i = 0; i < arrayImages.length(); i++) {
					images.add(arrayImages.getString(i));
				}
				this.images = images;
			}
			if (json.has(Constant.listOfRelativeApp)) {
				JSONArray arrayApps = json
						.getJSONArray(Constant.listOfRelativeApp);
				ArrayList<App> relativeApps = new ArrayList<App>();
				for (int i = 0; i < arrayApps.length(); i++) {
					relativeApps.add(new App(arrayApps.getJSONObject(i)));
				}
				this.relativeApps = relativeApps;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	@Override
	public String toString() {
		return "App [id=" + id + ", name=" + name + ", iconLink=" + iconLink
				+ ", rateNum=" + rateNum + ", price=" + price
				+ ", companyName=" + companyName + ", description="
				+ description + ", linkApk=" + linkApk + ", downloadNum="
				+ downloadNum + ", size=" + size + ", uploadDate=" + uploadDate
				+ ", relativeApps=" + relativeApps + ", images=" + images + "]";
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getIconLink() {
		return iconLink;
	}

	public void setIconLink(String iconLink) {
		this.iconLink = iconLink;
	}

	public int getRateNum() {
		return rateNum;
	}

	public void setRateNum(int rateNum) {
		this.rateNum = rateNum;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getLinkApk() {
		return linkApk;
	}

	public void setLinkApk(String linkApk) {
		this.linkApk = linkApk;
	}

	public int getDownloadNum() {
		return downloadNum;
	}

	public void setDownloadNum(int downloadNum) {
		this.downloadNum = downloadNum;
	}

	public float getSize() {
		return size;
	}

	public void setSize(float size) {
		this.size = size;
	}

	public Calendar getUploadDate() {
		return uploadDate;
	}

	public void setUploadDate(Calendar uploadDate) {
		this.uploadDate = uploadDate;
	}

	public ArrayList<App> getRelativeApps() {
		return relativeApps;
	}

	public void setRelativeApps(ArrayList<App> relativeApps) {
		this.relativeApps = relativeApps;
	}

	public ArrayList<String> getImages() {
		return images;
	}

	public void setImages(ArrayList<String> images) {
		this.images = images;
	}

	public Boolean getIsPay() {
		return isPay;
	}

	public void setIsPay(Boolean isPay) {
		this.isPay = isPay;
	}

	public Boolean getIsOnline() {
		return isOnline;
	}

	public void setIsOnline(Boolean isOnline) {
		this.isOnline = isOnline;
	}

	public Boolean getIsHot() {
		return isHot;
	}

	public void setIsHot(Boolean isHot) {
		this.isHot = isHot;
	}

	public String getPackageName() {
		return packageName;
	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getDetail() {
		return detail;
	}

	public void setDetail(String detail) {
		this.detail = detail;
	}

}
